<?php

namespace app\admin\controller;
use think\Db;

class User extends Base
{
    //
    public function index(){
    	$size=1;
        $title=input('param.')['mobile']??'';
    	$data=Db::table('sys_user')->where([['mobile', 'like', '%'.$title.'%']])->order('sort', 'desc')->paginate($size,false,[
    			'query'=>[
    				'mobile'=>$title
    			]
    		]);
    	$page=input('param.')['page']??1;
    	$this->assign('data',$data);
    	$this->assign('p',$page);
    	$this->assign('size',$size);
    	return $this->fetch();
    }
    // 添加用户
    public function add(){
    	$data=$this->request->param();
    	// 判断数据库是否存在当前用户
    	$res=Db::table('sys_user')->where('mobile',$data['mobile'])->find();
    	if($res) return json(['data'=>null,'msg'=>'用户已存在','code'=>500]);
    	// 用户账号
    	$data['user_no']=date('YmdHis',time()).rand(10,99);
    	$data['password']=md5($data['password']);
    	$res=Db::table('sys_user')->strict(false)->insert($data);
    	if($res) {
    		return json(['data'=>null,'msg'=>'添加成功','code'=>200]);
    	}else{
    		return json(['data'=>null,'msg'=>'参数错误','code'=>500]);
    	}

    }
    // 编辑用户
    public function update(int $id){
    	$data=$this->request->param();
    	$res=Db::table('sys_user')->where('id',$id)->field('mobile,real_name,remark,status_id')->update($data);
    	if($res) {
    		return json(['data'=>null,'msg'=>'编辑成功','code'=>200]);
    	}else{
    		return json(['data'=>null,'msg'=>'参数错误','code'=>500]);
    	}
    }
    // 删除用户
    public function delete(int $id){
    	$res=Db::table('sys_user')->where('id',$id)->delete();
    	if($res){
    		return json(['data'=>null,'msg'=>'删除成功','code'=>200]);
    	}else{
    		return json(['data'=>null,'msg'=>'参数错误','code'=>500]);
    	}
    }
    // 修改用户密码
    public function setPwd(int $id){
    	$arr['password']=md5($this->request->param()['password']);
    	$res=Db::table('sys_user')->where('id',$id)->update($arr);
    	if($res) {
    		return json(['data'=>null,'msg'=>'修改成功','code'=>200]);
    	}else{
    		return json(['data'=>null,'msg'=>'参数错误','code'=>500]);
    	}
    }
    // 角色列表
    public function setlist(){
    	$size=3;
        $title=input('param.')['role_name']??'';
    	$data=Db::table('sys_role')->where([['role_name', 'like', '%'.$title.'%']])->order('sort', 'desc')->paginate($size,false,[
    			'query'=>[
    				'role_name'=>$title
    			]
    		]);
    	return json(['data'=>$data,'size'=>$size]);
    }
    // 角色设置
    public function setrole(int $id){
    	$rids=$this->request->param()['rids']??'';
    	if(!$rids) return json(['data'=>null,'msg'=>'请选择角色后提交']);
    	$data=explode(',',$rids);
    	$arr=[];
    	foreach ($data as $k => $v) {
    		# code...
    		$arr[$k]['role_id']=$v;
    		$arr[$k]['user_id']=$id;
    	}
    	Db::table('sys_role_user')->where('user_id',$id)->delete();
    	$res=Db::table('sys_role_user')->insertAll($arr);
    	if($res) {
    		return json(['data'=>null,'msg'=>'设置成功','code'=>200]);
    	}else{
    		return json(['data'=>null,'msg'=>'参数错误','code'=>500]);
    	}
    }
}